//统计课程数和总价格
var calculateCart = function() {
    var totalCost = 0; //总价
    var courseCount = $("input:checked[aid]").length; //选中的课程总数
    var totalCourseCount = $("input:checkbox[aid]").length; //购物车课程总数
    $("input:checked[aid]").each(function() {

        totalCost += parseFloat($('#real_cost_' + $(this).attr('aid')).text());

    });
    if (courseCount < totalCourseCount) {
        $('#checkall1, #checkall2').attr('checked', false);
    } else if (courseCount == totalCourseCount) {
        $('#checkall1, #checkall2').attr('checked', true);
    }
    $('#course_count').text(courseCount);
    $('#total_cost').text(totalCost.toFixed(2));
};

$(function() {
    //选择课程
    $("input:checkbox[aid]").click(function() {
        calculateCart();
    });
    //全选
    $("#checkall1, #checkall2").click(function() {
        if ($(this).attr('checked')) {
            $("input:checkbox").attr("checked", true);
        } else {
            $("input:checkbox").attr("checked", false);
        }
        calculateCart();
    });
    //单个删除
    $(".deleteCart").click(function() {
        var aid = $(this).attr('aid');
        $.confirmBoxy({
            message: '你确定要从购物车中移除该课程吗？',
            ok: function() {
                var loading = null;
                $.ajax({
                    url: '/user/cart/del',
                    data: 'ids=' + aid,
                    type: 'post',
                    dataType: 'json',
                    beforeSend: function() {
                        loading = $.tipsBoxy({
                            type: "loading",
                            mask: true,
                            message: "操作中，请稍后"

                        });
                    },
                    success: function(ret) {
                        loading.cancel();
                        if (ret.code == 0) {
                            window.location.reload();
                        } else {
                            $.alertBoxy({
                                message: ret.data
                            });
                        }
                    }
                });
            }
        });
        return false;
    });
    //批量删除
    $(".batch_del").click(function() {
        var ids = new Array();
        $("input:checked[aid]").each(function() {
            ids.push($(this).attr('aid'));
        });
        $.confirmBoxy({
            width: 368,
            message: '确实要将所有选中的课程从购物车中移除吗？',
            ok: function() {
                var loading = null;
                $.ajax({
                    url: '/user/cart/del',
                    data: 'ids=' + ids.join(','),
                    type: 'post',
                    dataType: 'json',
                    beforeSend: function() {
                        loading = $.tipsBoxy({
                            type: "loading",
                            mask: true,
                            message: "操作中，请稍后"

                        });
                    },
                    success: function(ret) {
                        loading.cancel();
                        if (ret.code == 0) {
                            window.location.reload();
                        } else {
                            $.alertBoxy({
                                message: ret.data
                            });
                        }
                    }
                });
            }
        });
        return false;
    });
    //提交订单
    $("#postBtn").click(function() {
        var params = new Array();
        $("input:checked[aid]").each(function() {
            var cobj = $(this);
            var cartId = cobj.attr('aid'); //购物车编号           
            params.push(cartId);
        });
        if (params.length == 0) {
            $.alertBoxy({
                message: "请选择要购买的课程"

            });
            return false;
        }
        var loading = null;
        $.ajax({
            url: '/order/submit',
            data: 'ids=' + params.join(','),
            type: 'post',
            dataType: 'json',
            beforeSend: function() {
                loading = $.tipsBoxy({
                    type: "loading",
                    mask: true,
                    message: "操作中，请稍后"

                });
            },
            success: function(ret) {
                loading.cancel();
                if (ret.code == 0) {
                    window.location.href = "/order/pay/" + ret.id;
                } else {
                    $.alertBoxy({
                        message: ret.data
                    });
                }
            }
        });
    });
    calculateCart();
});